package interfaceAA;

import g2RedBlackTree.DataTTree;
import g2RedBlackTree.MobiTTree;
import g2RedBlackTree.RedBlackNodeData;
import g2Solution.MySolution;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;

import redBlackTree.RedBlackTree;

import loadingFile.KeyValuePair;
import loadingFile.LoadingFile;


public class MobiFrame extends JFrame {

	private JPanel contentPane;

	public String sequenceNumber = "";
	private String source = "";
	private String destination = "";
	private String content = "";
	
	DataTTree words = null;
	MobiInterface testMobi;
	static RedBlackTree rBT;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		//main1(args);
		questionC();
	}
	public static void questionC(){
		MobiTTree tree = new MobiTTree();
		
		LoadingFile loadingFile = new LoadingFile("src/loadingFile/noun");
		Vector<KeyValuePair> valueFromDictionnary  = loadingFile.loading();
		
		for (KeyValuePair keyValuePair : valueFromDictionnary) {
			try {
				tree.insert(String.valueOf(keyValuePair.getKey())).getWords().insert((String) keyValuePair.getValue());
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		MySolution myS = new MySolution();
		
		System.out.println(myS.Qc(tree, "244"));
	}
	public static void main1(String[] args) {
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MobiFrame frame = new MobiFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});  

		LoadingFile loadingFile = new LoadingFile("src/loadingFile/noun");
		Vector<KeyValuePair> valueFromDictionnary  = loadingFile.loading();
		
		rBT = new RedBlackTree();
		boolean exist;
		
		for (KeyValuePair keyValuePair : valueFromDictionnary) {
			try {
				//System.out.println( (String) keyValuePair.getValue() );
				exist = rBT.insert((String) keyValuePair.getKey());
				DataTTree words = rBT.findWordsArray((String) keyValuePair.getKey());
				if(words != null){
					words.insert((String) keyValuePair.getValue());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}         
	}

	/**
	 * Create the frame.
	 */
	public MobiFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		testMobi = new MobiInterface();
		contentPane.add(testMobi, BorderLayout.NORTH);
		
		/**
		 * Add an action listener on the select button
		 */
		testMobi.mobiFunction1.btnSelect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//226723 = aboard
				//22225 = aback
				destination = testMobi.jTextArea1.getText();
				if(source != "" && destination != "")
				{
					LoadingFile obj = new LoadingFile("noun.txt");
					String sourceNumber = obj.findNumberFromWord(source);
					String destinationNumber = obj.findNumberFromWord(destination.split(" ")[0]);
					String resultNumber = Qb(sourceNumber,destinationNumber);
					testMobi.jTextArea1.setText("The shortest path from '" + destination.split(" ")[0] + "' to '" + destination.split(" ")[1] +"' is: " + resultNumber);
				}
			}
		});
		
		/**
		 * Add an action listener on the abc button
		 */
		testMobi.mobiFunction1.btnSpace.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//226723 = aboard
				//22225 = aback
				//sourceWord = testMobi.jTextArea1.getText().trim();
				//JOptionPane.showMessageDialog(contentPane,"This is the path : " + Qb("22225","226723"));
				source = testMobi.jTextArea1.getText().concat(" ");
				sequenceNumber = "";
				testMobi.jTextArea1.setText(source);
				//testMobi.jTextArea1.setText("");
			}
		});
		
		
		/**
		 * Add an action listener on the abc button
		 */
		testMobi.mobiFunction1.btnabc.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("2");
			}
		});
		
		/**
		 * Add an action listener on the def button
		 */
		testMobi.mobiFunction1.btndef.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("3");
			}
		});
		
		/**
		 * Add an action listener on the ghi button
		 */
		testMobi.mobiFunction1.btnghi.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("4");
			}
		});
		
		/**
		 * Add an action listener on the jkl button
		 */
		testMobi.mobiFunction1.btnjkl.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("5");
			}
		});
		
		/**
		 * Add an action listener on the mno button
		 */
		testMobi.mobiFunction1.btnmno.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("6");
			}
		});
		
		/**
		 * Add an action listener on the pqrs button
		 */
		testMobi.mobiFunction1.btnpqrs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("7");
			}
		});
		
		/**
		 * Add an action listener on the tuv button
		 */
		testMobi.mobiFunction1.btntuv.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("8");
			}
		});
		
		/**
		 * Add an action listener on the wxyz button
		 */
		testMobi.mobiFunction1.btnwxyz.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addToSequence("9");
			}
		});
		
		/**
		 * Add an action listener on the Star button
		 */
		testMobi.mobiFunction1.btnStar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				nextWordIndex();
			}
		});
		/**
		 * Add an action listener on the Next button
		 */
		testMobi.mobiFunction1.btnNext.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				nextWordIndex();
			}
		});
		/**
		 * Add an action listener on the Prev button
		 */
		testMobi.mobiFunction1.btnPrev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				prevWordIndex();
			}
		});
		/**
		 * Add an action listener on the clear button
		 */
		testMobi.mobiFunction1.btnClear.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				remToSequence();
			}
		});
	}
	
	
	/**
	 * add a number to the actual sequence
	 * @param seq
	 */
	public void addToSequence(String seq){
		addToSequence1(seq);
	}
	public void addToSequence1(String seq){
		sequenceNumber = sequenceNumber + seq;
		reInitWordIndex();
		refreshTextArea();
	}
	/**
	 * remove the last number
	 */
	public void remToSequence1(){
		remToSequence();
	}
	public void remToSequence(){
		if(sequenceNumber.length() > 0){
			sequenceNumber = sequenceNumber.substring(0, sequenceNumber.length() - 1);
		}
		else {
			if(source.length() > 0)
				source = source.substring(0, source.length() - 1);
		}
		reInitWordIndex();
		refreshTextArea();
	}
	
	/**
	 * reintialisation of the word index
	 */
	public void reInitWordIndex(){
		
		words = rBT.findWordsArray(sequenceNumber);
		
		//if(words != null && words.size() == 0){
			//words = rBT.findBiggerPossibilty(sequenceNumber);
		//}
		
		words.initCusor();
	}
	
	/**
	 * next word with this sequence number
	 */
	public void nextWordIndex(){
		refreshTextArea();
	}
	/**
	 * next word with this sequence number
	 */
	public void prevWordIndex(){
		refreshTextArea(true);
	}
	
	public void refreshTextArea(){
		refreshTextArea(false);
	}
	/**
	 * refresh the textarea regarding to the actual number sequence
	 */
	public void refreshTextArea(boolean goBack){
		String result = null;
		if (words != null){
			if (goBack)
				result = words.prev();
			else {
				result = words.next();
			}
		} 
		if (result == null){
			//words = rBT.findBiggerPossibilty(sequenceNumber);
			//words.initCusor();
			if (words != null){
				result = sequenceNumber;
			} 
			else{
				result = sequenceNumber;
			}
		}
		testMobi.jTextArea1.setText(source + result);
	}
	
	public String Qb(String key1, String key2){
		return rBT.Qb(key1, key2);
	}
	public String Qc(String key1){
		return "Not implement yet";
	
	}
}
